package com.qd.admin.controller.dict;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.domain.entity.dict.DictTypeDTO;
import com.qd.common.sys.enums.OperatorType;
import com.qd.system.service.dict.DictDataService;
import com.qd.system.service.dict.DictTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 数据字典类型
 */
@RestController
@Validated
@RequiredArgsConstructor
@Slf4j
public class DictTypeController {

    private final DictTypeService dictTypeService;
    private final DictDataService dictDataService;

    /**
     * 新增类型
     *
     * @return 新增数据
     */
    @PreAuthorize("@ps.hasPermission('system:dict:save')")
    @PostMapping(value = "/sys/sysDictType/save")
    @OperatorInfo(title = "添加码表类型", operator = OperatorType.INSERT)
    public DictTypeDTO save(@Validated(DefaultGroup.class) @RequestBody DictTypeDTO dictTypeDTO) {
        dictTypeDTO.setAddTime(LocalDateTime.now());
        return dictTypeService.save(dictTypeDTO);
    }

    /**
     * 修改类型
     *
     * @return true or false
     */
    @PreAuthorize("@ps.hasPermission('system:dict:edit')")
    @PutMapping(value = "/sys/sysDictType/update")
    @OperatorInfo(title = "修改码表类型", operator = OperatorType.UPDATE)
    public ApiResultWrapper<Object> update(@Validated({DefaultGroup.class}) @RequestBody DictTypeDTO dictTypeDTO) {
        dictTypeDTO.setUpdateTime(LocalDateTime.now());
        return ApiResult.isSuccess(dictTypeService.updateById(dictTypeService.dtoToModel(dictTypeDTO)));
    }

    /**
     * 删除类型
     */
    @PreAuthorize("@ps.hasPermission('system:dict:delete')")
    @DeleteMapping(value = "/sys/sysDictType/delete")
    @OperatorInfo(title = "删除码表类型", operator = OperatorType.DELETE)
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            dictDataService.removeByTypeId(ids.get(0));
        } else {
            dictDataService.removeByTypeIds(ids);
        }
        return ApiResult.success();
    }

    /**
     * 类型详情
     *
     * @return 详情
     */
    @GetMapping(value = "/sys/sysDictType/getById")
    public DictTypeDTO getById(@Validated @NotBlank(message = "类型ID不能为空") String typeId) {
        return dictTypeService.getByIdDto(typeId);
    }

    /**
     * 类型列表
     *
     * @return 列表
     */
    @GetMapping(value = "/sys/sysDictType/page")
    public IPage<DictTypeDTO> page(DictTypeDTO dictTypeDTO) {
        return dictTypeService.page(PageUtil.buildPage(dictTypeDTO), dictTypeDTO);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ps.hasPermission('system:dict:export')")
    @GetMapping(value = "/sys/sysDictType/export")
    public void export(DictTypeDTO dictTypeDTO) {
        long startTime = System.currentTimeMillis();
        dictTypeService.export(dictTypeDTO);
        long endTime = System.currentTimeMillis();
        log.info("数据字典类型导出,总耗时: {}s", (endTime - startTime) / 1000);
    }
}